package com.scwatch.core.web.back;

import com.scwatch.common.orm.RowSide;
import com.scwatch.common.web.Servlets;
import com.scwatch.core.constant.Constants;
import com.scwatch.core.domain.Program;
import com.scwatch.core.domain.User;
import com.scwatch.core.service.OperationLogService;
import com.scwatch.core.service.ProgramService;
import com.scwatch.core.support.Context;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

import static com.scwatch.core.constant.Constants.*;

/**
 * Created by rh006 on 2017/7/21.
 */


@Controller
@RequestMapping("/core/program")
public class ProgramController {

    private static final Logger logger = LoggerFactory
            .getLogger(ProgramController.class);

    @RequestMapping("list.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:program:list")
    public String list(
            @PageableDefault(sort = "id", direction = Sort.Direction.DESC) Pageable pageable,
            HttpServletRequest request, org.springframework.ui.Model modelMap) {
        Map<String, String[]> params = Servlets.getParamValuesMap(request,
                Constants.SEARCH_PREFIX);
        Page<Program> pagedList = programService.findAll(params, pageable);
        modelMap.addAttribute("pagedList", pagedList);
        return "core/program/program_list";
    }

    @RequestMapping("create.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:program:create")
    public String create(Integer id, org.springframework.ui.Model modelMap) {
        if (id != null) {
            Program bean = programService.get(id);
            modelMap.addAttribute("bean", bean);
        }
        modelMap.addAttribute(OPRT, CREATE);
        return "core/program/program_form";
    }

    @RequestMapping("edit.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:program:edit")
    public String edit(
            Integer id,
            Integer position,
            @PageableDefault(sort = "id", direction = Sort.Direction.DESC) Pageable pageable,
            HttpServletRequest request, org.springframework.ui.Model modelMap) {
        Program bean = programService.get(id);
        Map<String, String[]> params = Servlets.getParamValuesMap(request,
                Constants.SEARCH_PREFIX);
        RowSide<Program> side = programService.findSide(params, bean, position,
                pageable.getSort());
        modelMap.addAttribute("bean", bean);
        modelMap.addAttribute("side", side);
        modelMap.addAttribute("position", position);
        modelMap.addAttribute(OPRT, EDIT);
        return "core/program/program_form";
    }

    @RequestMapping("save.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:program:save")
    public String save(Program bean, String redirect,
                       HttpServletRequest request, RedirectAttributes ra) {
        Integer siteId = Context.getCurrentSiteId();
        User user = Context.getCurrentUser();
        bean.setTime(new Date());
        bean.setUser(user);
        programService.save(bean, siteId, user);

        logService.operation("opr.program.add", bean.getName(), null,
                bean.getId(), request);
        logger.info("save Program, name={}.", bean.getName());
        ra.addFlashAttribute(MESSAGE, SAVE_SUCCESS);
        if (Constants.REDIRECT_LIST.equals(redirect)) {

            return "redirect:list.do";
        } else if (Constants.REDIRECT_CREATE.equals(redirect)) {
            return "redirect:create.do";
        } else {
            ra.addAttribute(bean.getId());
            return "redirect:edit.do";
        }
    }

    @RequestMapping("update.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:program:update")
    public String update(@ModelAttribute("bean") Program bean,
                         Integer position, String redirect, HttpServletRequest request,
                         RedirectAttributes ra) {
        programService.update(bean);
        logService.operation("opr.program.edit", bean.getName(), null,
                bean.getId(), request);
        logger.info("update Program, name={}.", bean.getName());
        ra.addFlashAttribute(MESSAGE, SAVE_SUCCESS);
        if (Constants.REDIRECT_LIST.equals(redirect)) {
            return "redirect:list.do";
        } else {
            ra.addAttribute("id", bean.getId());
            ra.addAttribute("position", position);
            return "redirect:edit.do";
        }
    }

    @RequestMapping("delete.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:program:delete")
    public String delete(Integer[] ids, HttpServletRequest request,
                         RedirectAttributes ra) {
        Program[] beans = programService.delete(ids);
        for (Program bean : beans) {
            logService.operation("opr.program.delete", bean.getName(),
                    null, bean.getId(), request);
            logger.info("delete Program, name={}.", bean.getName());
        }
        ra.addFlashAttribute(MESSAGE, DELETE_SUCCESS);
        return "redirect:list.do";
    }

    @ModelAttribute("bean")
    public Program preloadBean(@RequestParam(required = false) Integer oid) {
        return oid != null ? programService.get(oid) : null;
    }

    @Autowired
    private OperationLogService logService;
    @Autowired
    private ProgramService programService;


}
